<?php

class menu_process extends CI_Model {//这里关于admin表的一些操作和登录判定

    const Statustrue = True;
    const Statusflase = False;
    const Status_type_single = 0;
    const Status_type_list = 1;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('file_upload'); //引入类库
    }

    function menu_list() {
        $sql = "select * from menu";
        $query = $this->db->query($sql);
        return $res = $query->result_array();
    }
    
    
    function menu_front_list() {//获取前台菜单
         $sql = "select * from menu order by morder";
        $query = $this->db->query($sql);        
         return $res = $query->result_array();
        
    }
    

    function add_menu($post_array) {
        $post_array = $this->is_menu_res_repeat($post_array);
        $insertsql = "insert into menu(name,banner,banner_url,
   content,controller_name,morder,type
)values(\"{$post_array["name"]}\",\"{$post_array["full_path"]}\",\"{$post_array["bannerurl"]}\",\"{$post_array["content"]}\",
    \"{$post_array["controller_name"]}\",\"{$post_array["order"]}\",\"{$post_array["type"]}\")";
        $this->db->query($insertsql);
    }

    function update_menu($id) {
        //首先更新图片
        $config['upload_path'] = './uploads/banner'; //设定上传存储路径
        $data = $this->file_upload->upLoadfile($config, "banner");
        if (isset($data)) {
            $siteurl = site_url(); //$siteurl .
            $config["source_image"] = $data["full_path"];
            $config["new_image"] = './uploads/banner/thum';
            $this->file_upload->process_img($config);
            $post_array["full_path"] = "{$data["file_name"]}"; //获得新的图片名称对应数据库中banner字段            
            //同步更新数据库记录
            $_POST["content"]=htmlspecialchars($_POST["content"]);
            $update_sql = "update menu set name=\"{$_POST["name"]}\", banner=\"{$post_array["full_path"]}\",banner_url=\"{$_POST["banner_url"]}\",content=\"{$_POST["content"]}\",morder=\"{$_POST["order"]}\"
                where id=\"{$id}\"";
        } else {            
            //不更新图片链接，同步更新数据库
                 $_POST["content"]=htmlspecialchars($_POST["content"]);
            $update_sql = "update menu set name=\"{$_POST["name"]}\",banner_url=\"{$_POST["banner_url"]}\",content=\"{$_POST["content"]}\",morder=\"{$_POST["order"]}\"
                where id=\"{$id}\"";
        }
        $query = $this->db->query($update_sql);
    }

    function is_menu_res_repeat($post_array) {//对于传入的数值做判定
//        echo menu_process::Status_type_list ;
//        exit;
        if (is_numeric($post_array["order"])) {
            $sql = "select * from menu where  controller_name=\"{$post_array["controller_name"]}\" or morder=\"{$post_array["order"]}\"  ";   //查重
            $query = $this->db->query($sql);
            $res = $query->result_array();
            if (!empty($res)) {
                redirect("admin/menu_manag");
            } else {//没有重复的记录可以插入
                if ($post_array["type"] == menu_process::Status_type_single) {
                    $post_array["content"] = htmlspecialchars($post_array["content"]); //这里html实体转义,ENT_QUOTES
                    return $post_array;
                } else {
                    $post_array["bannerurl"] = "#";
                    $post_array["full_path"] = "#";
                    $post_array["content"] = "#";

                    return $post_array;
                }
            }
        } else {
            redirect("admin/menu_manag");
        }
    }

}

?>
